package com.hema.auction.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hema.auction.R;
import com.hema.auction.adapter.OldOrdersAdapter;
import com.hema.auction.adapter.SunSquareAdapter;
import com.hema.auction.base.BaseFragment;
import com.hema.auction.bean.SunSquareInfo;
import com.hema.auction.http.HttpTag;
import com.hema.auction.http.biz.HttpManager;
import com.hema.auction.utils.Constant;
import com.hema.auction.widget.view.EmptyView;
import com.hema.auction.widget.view.MyLoadMoreView;
import com.hema.auction.widget.view.RuleView;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * author by john
 */

public class GoodDetailFragment extends BaseFragment {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    private SunSquareAdapter sunSquareAdapter;
    private List<SunSquareInfo> infos = new ArrayList<>();
    private int position;

    private OldOrdersAdapter oldOrdersAdapter;
    private List<DealInfo> dealInfos = new ArrayList<>();
    private SwipeRefreshLayout refreshLayout;
    private boolean isRefresh = true;
    private EmptyView emptyView;
    private String goodId;

    @Override
    protected int getLayout() {
        return R.layout.fragment_good_detail;
    }

    public static GoodDetailFragment newInstance(int position, String goodId) {
        Bundle bundle = new Bundle();
        bundle.putInt("position", position);
        bundle.putString(Constant.EXTRA_GOOD_ID, goodId);
        GoodDetailFragment fragment = new GoodDetailFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    public void refreshLayout(SwipeRefreshLayout refreshLayout) {
        this.refreshLayout = refreshLayout;
    }

    @Override
    protected void init() {
        super.init();
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        position = getArguments().getInt("position");
        goodId = getArguments().getString(Constant.EXTRA_GOOD_ID);
        switch (position) {
            case 0://晒单
                recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity())
                        .size(1).showLastDivider().color(getResources().getColor(R.color.colorDivider)).build());
                emptyView = new EmptyView(getActivity());
                emptyView.setViewType(EmptyView.GOOD_DETAIL_SUN);
                sunSquareAdapter = new SunSquareAdapter(infos);
                sunSquareAdapter.setShowTitle(false);
                sunSquareAdapter.setLoadMoreView(new MyLoadMoreView());
                sunSquareAdapter.setEmptyView(emptyView);
                recyclerView.setAdapter(sunSquareAdapter);
                sunSquareAdapter.setOnLoadMoreListener(this, recyclerView);
                HttpManager.getInstance(getContext()).getShowsByGood(goodId, pageIndex, pageSize, this);
                break;
            case 1://往期成交
                emptyView = new EmptyView(getActivity());
                emptyView.setViewType(EmptyView.OTHER);
                oldOrdersAdapter = new OldOrdersAdapter(dealInfos);
                oldOrdersAdapter.setLoadMoreView(new MyLoadMoreView());
                oldOrdersAdapter.setEmptyView(emptyView);
                recyclerView.setAdapter(oldOrdersAdapter);
                oldOrdersAdapter.setOnLoadMoreListener(this, recyclerView);
                HttpManager.getInstance(getContext()).getOldrders(goodId, pageIndex, pageSize, this);
                break;
            case 2://竞拍规则
                oldOrdersAdapter = new OldOrdersAdapter(dealInfos);
                recyclerView.setAdapter(oldOrdersAdapter);
                RuleView ruleView = new RuleView(getActivity());
                oldOrdersAdapter.addHeaderView(ruleView);
                break;
        }
    }

    public RecyclerView getRecyclerView() {
        return recyclerView;
    }


    public void refresh(String goodId) {
        this.goodId = goodId;
        isRefresh = true;
        pageIndex = 1;
        switch (position) {
            case 0:
                HttpManager.getInstance(getContext()).getShowsByGood(goodId, pageIndex, pageSize, this);
                break;
            case 1:
                HttpManager.getInstance(getContext()).getOldrders(goodId, pageIndex, pageSize, this);
                break;
        }
    }

    @Override
    protected void onLoadMore() {
        super.onLoadMore();
        isRefresh = false;
        pageIndex++;
        switch (position) {
            case 0:
                HttpManager.getInstance(getContext()).getShowsByGood(goodId, pageIndex, pageSize, this);
                break;
            case 1:
                HttpManager.getInstance(getContext()).getOldrders(goodId, pageIndex, pageSize, this);
                break;
        }
    }

    @Override
    protected void onSuccess(HttpTag httpTag, Call call, JSONObject result) {
        super.onSuccess(httpTag, call, result);
        switch (httpTag) {
            case TAG_GET_SHOWS_BY_GOOD:
                try {
                    if (result.getInt("errcode") == 0) {
                        List<SunSquareInfo> newData = new Gson().fromJson(result.getJSONArray("data").toString(), new TypeToken<List<SunSquareInfo>>() {
                        }.getType());
                        if (newData != null) {
                            if (isRefresh) {
                                sunSquareAdapter.setNewData(newData);
                                if (newData.size() < pageSize) {
                                    sunSquareAdapter.setEnableLoadMore(false);
                                } else {
                                    sunSquareAdapter.setEnableLoadMore(true);
                                }
                            } else {
                                sunSquareAdapter.addData(newData);
                                sunSquareAdapter.loadMoreComplete();
                            }
                            if (newData.size() < pageSize) {
                                sunSquareAdapter.loadMoreEnd();
                            }
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case TAG_GET_OLD_ORDERS:
                try {
                    if (result.getInt("errcode") == 0) {
                        List<DealInfo> newData = new Gson().fromJson(result.getJSONArray("data").toString(), new TypeToken<List<DealInfo>>() {
                        }.getType());
                        if (isRefresh) {
                            oldOrdersAdapter.setNewData(newData);
                            if (newData.size() < pageSize) {
                                oldOrdersAdapter.setEnableLoadMore(false);
                            } else {
                                oldOrdersAdapter.setEnableLoadMore(true);
                            }
                        } else {
                            oldOrdersAdapter.addData(newData);
                            oldOrdersAdapter.loadMoreComplete();
                        }
                        if (newData.size() < pageSize) {
                            oldOrdersAdapter.loadMoreEnd();
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    @Override
    protected void onFinish(HttpTag httpTag) {
        super.onFinish(httpTag);
        refreshLayout.setRefreshing(false);
        isRefresh = false;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }
}
